package cn.lyh.gulimall.product.controller;

import cn.lyh.gulimall.product.entity.PmsAttr;
import cn.lyh.gulimall.product.service.PmsAttrService;
import cn.lyh.gulimall.product.vo.AttrRespVo;
import cn.lyh.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.PageUtils;
import utils.R;

import java.util.Arrays;
import java.util.Map;

/**
 * @author lyh
 * @version 1.0
 * @since 2021/4/13
 */
@RestController
@RequestMapping("/product/attr/")
public class PmsAttrController {


	@Autowired
	PmsAttrService attrService;

	@PostMapping("/save")
	public R save(@RequestBody AttrVo pmsAttr){
		attrService.saveAttr(pmsAttr);
		return R.ok();
	}

	@GetMapping("/{attrType}/list/{catelogId}")
	public R baseAttrList(@RequestParam Map<String, Object> params,
						  @PathVariable("catelogId") Long catelogId,
						  @PathVariable String attrType){
		PageUtils pageUtils = attrService.quertBaseAttrPage(params,catelogId,attrType);
		return R.ok().put("page",pageUtils);
	}

	@GetMapping("/info/{attrId}")
	public R getAttrInfoByAttrId(@PathVariable("attrId") Long attrId){
		AttrRespVo attrRespVo = attrService.getInfoByAttrId(attrId);
		return R.ok().put("attr",attrRespVo);
	}

	@PostMapping("/update")
	public R update(@RequestBody AttrVo attrVo){
		attrService.updateAttr(attrVo);
		return R.ok();
	}

	@PostMapping("/delete")
	public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));
		return R.ok();
	}



}
